/**
 * Project Name:trustsql-membercenter-util
 * File Name:RequestUtil.java
 * Package Name:com.tencent.trustsql.membercenter.util
 * Date:2017年9月8日上午11:07:57
 * Copyright (c) 2017, Tencent All Rights Reserved.
 *
*/

package com.tencent.trustsql.idm.demo.util;

import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
import com.tencent.trustsql.idm.demo.bean.*;
import com.tencent.trustsql.sdk.TrustSDK;

/**
 * ClassName:RequestUtil <br/>
 * Date:     2017年9月8日 上午11:07:57 <br/>
 * @author   ronyyang
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
public class RequestUtil {
	//私钥
	static String prvKey = "2r5KM0GFlyVJaZzyDVh2fCzwNykyjINJDTUFQKgMStw=";
	//公钥
	static String pubKey = "AttKPLaTisB35xEHTpjxzrMQXj72F7oLJZYfEBqYXZ8Z";
	//签名
	//static String mchSign = "MEUCIQD7rT210RYN5L/HZIv8qgZQerYdyNwcSxKWwfANWaN1rwIgLQhhfzR03GQvkAWxhDbI2svJKTBGWe0jBmRQ5nCbmQY=";

	//查询用户参数
	public static String generateQueryAllAccountRequest() throws Exception {
		
		RequestData req = new RequestData();
		req.setMch_id("gb2af850d5722c68");
		req.setProduct_code("productA");
		req.setSeq_no(getUUID());
		req.setType("sign");
		req.setTime_stamp(System.currentTimeMillis());

		AccountCert account = new AccountCert();
		account.setUser_id("1221838d");
		req.setReq_data(JSONObject.toJSONString(account));

		String signSrc = SignUtil.genSignSrc(req);
		String sign = TrustSDK.signString(prvKey, signSrc.getBytes(),false);
		req.setSign(sign);
		return JSONObject.toJSONString(req);
	}
	
	public static String generateQueryAccountRequest() throws Exception {
		RequestData req = new RequestData();
		req.setMch_id("123456789012");
		req.setProduct_code("productA");
		req.setSeq_no("12345678901234567890123456789015");
		req.setType("sign");
		AccountCert account = new AccountCert();
		account.setUser_id("1");
		account.setAccount_address("17SU4AXff9bXuPseaH6FXV9gcJPhUjYbc6");
		req.setReq_data(JSONObject.toJSONString(account));
		req.setTime_stamp(System.currentTimeMillis());
		String prvKey = "FrujLfhuf6UDRK4DkKMykE0stwqWw9pkeq5DhuxvUCs=";
		String signSrc = SignUtil.genSignSrc(req);
		String sign = TrustSDK.signString(prvKey, signSrc.getBytes(),false);
		req.setSign(sign);
		return JSONObject.toJSONString(req);
	}

	//创建用户账户参数
	public static String generateRegisterAccountRequest() throws Exception {
		RequestData req = new RequestData();
		req.setMch_id("gb2af850d5722c68");
		req.setProduct_code("productA");
		req.setSeq_no(getUUID());
		req.setType("sign");
		req.setTime_stamp(System.currentTimeMillis());

		AccountCert account = new AccountCert();
		account.setUser_id("1221838d");
		account.setPublic_key(pubKey);
		req.setReq_data(JSONObject.toJSONString(account));
		String signSrc = SignUtil.genSignSrc(req);
		System.out.println(signSrc);
		String sign = TrustSDK.signString(prvKey, signSrc.getBytes(),false);
		req.setSign(sign);
		System.out.println(sign);
		return JSONObject.toJSONString(req);
	}
	
	public static String generateQueryUserRequeset() throws Exception {
		RequestData req = new RequestData();
		req.setMch_id("123456789012");
		req.setProduct_code("productA");
		req.setSeq_no("123456789d1223567440123d5df51033");
		req.setType("sign");
		UserCert user = new UserCert();
		user.setUser_id("1");
		req.setReq_data(JSONObject.toJSONString(user));
		req.setTime_stamp(System.currentTimeMillis());
		String prvKey = "FrujLfhuf6UDRK4DkKMykE0stwqWw9pkeq5DhuxvUCs=";
		String signSrc = SignUtil.genSignSrc(req);
		String sign = TrustSDK.signString(prvKey, signSrc.getBytes(),false);
		req.setSign(sign);
		return JSONObject.toJSONString(req);
	}

	//注册用户参数
	public static String generateRegisterUserRequest() throws Exception {
		RequestData req = new RequestData();
		req.setMch_id("gb2af850d5722c68");
		req.setProduct_code("productA");
		req.setSeq_no(getUUID());
		req.setType("sign");
		UserCert user = new UserCert();
		user.setUser_id("1221838d");
		user.setUser_fullName("哈哈");
		user.setPublic_key("BEqNd3ptAesDoytwFTTFNQaY8r+Xc68YlPFQ2BvFLAwqHcodpxipYlpOoHffhi33VLfPHxKs+tp8zbuTaGM77RY=");
		req.setReq_data(JSONObject.toJSONString(user));

		Date date = new Date();
		req.setTime_stamp(date.getTime());

		String signSrc = SignUtil.genSignSrc(req);
		System.out.println("待签名字符串："+signSrc);
		String sign = TrustSDK.signString(prvKey, signSrc.getBytes(),false);
		req.setSign(sign);
		System.out.println(sign);
		System.out.println(JSONObject.toJSONString(req));
		return JSONObject.toJSONString(req);
	}


	/**
	 * 得到32位的UUID
	 *
	 * @return
	 */
	public static String getUUID() {
		String uuid = java.util.UUID.randomUUID().toString().replaceAll("-", "");
		return uuid;
	}





	//构建资产发行申请参数
	public static  String generateAssetsApplyRequest() throws Exception{
		//公共参数
		DigitalAssetsRequestData digitalAssetsRequestData = new DigitalAssetsRequestData();
		digitalAssetsRequestData.setVersion("1.0");
		digitalAssetsRequestData.setSign_type("ECDSA");
		digitalAssetsRequestData.setMch_id("gb2af850d5722c68");


		//业务参数

		//digitalAssetsRequestData.setChain_id("ch_tencent_test");
		digitalAssetsRequestData.setChannel_id("ch_tencent_test");
		digitalAssetsRequestData.setSource_id("1");
		digitalAssetsRequestData.setOwner_account("1NPYQs22GiEoo9vbZEHsBWjg8HfTpHonF");
		digitalAssetsRequestData.setAsset_type(1);
		digitalAssetsRequestData.setAmount(10);
		digitalAssetsRequestData.setUnit("fen");

		JSONObject contentJson = new JSONObject();
		contentJson.put("name","资产1");
		contentJson.put("count",10);
		digitalAssetsRequestData.setContent(contentJson);

		Date date = new Date();

		digitalAssetsRequestData.setTimestamp(date.getTime());


		String signSrc = SignUtil.genSignSrc(digitalAssetsRequestData);
		System.out.println("待签名字符串："+signSrc);

		String signString = TrustSDK.signString(prvKey,signSrc.getBytes("UTF-8"),false);


		digitalAssetsRequestData.setMch_sign(signString);

		digitalAssetsRequestData.setNode_id("nd_tencent_test1");
		digitalAssetsRequestData.setLedger_id("ld_tencent_iss");

		return JSONObject.toJSONString(digitalAssetsRequestData);
	}
}

